package com.ls.io.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class NIOClient {


    public static void main(String[] args) throws IOException, InterruptedException {
        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.configureBlocking(true);

        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(1024);
        byteBuffer.put("hello".getBytes());


        socketChannel.connect(new InetSocketAddress("127.0.0.1", 8088));
        if(socketChannel.finishConnect()){
            System.out.println("222");
            byteBuffer.flip();
            socketChannel.write(byteBuffer);

            byteBuffer.clear();

            socketChannel.read(byteBuffer);
            byteBuffer.flip();
            byte[] bytes = new byte[byteBuffer.limit()];
            System.out.println(new String(bytes));

//            socketChannel.close();

        }
        TimeUnit.SECONDS.sleep(1);

        socketChannel.close();

        TimeUnit.SECONDS.sleep(2);
    }

}
